//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    diceConst: ["/pages/images/01.png", "/pages/images/02.png", "/pages/images/03.png", "/pages/images/04.png", "/pages/images/05.png", "/pages/images/06.png"], //骰子点数
    dicePng: ["/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png"],
    initPng: ["/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png"], //未知点数
    historyCount: 0, //历史点数
    currentCount: 0, //当前点数
    actionSheetHidden: true, //列表隐藏标志
    diceCount: 6, //骰子数
    displayFlag: ["", "", "", "", "", ""], //不显示
    genCount: [] //生成的骰子点数
  },
  //事件处理函数
  bindViewTap: function() {

  },
  onLoad: function() {
    var displayFlag = [];
    for (var i = 0; i < 5; i++) {
      displayFlag[i] = "none";
    }
    this.setData({
      displayFlag: displayFlag,
      genCount: []
    });
  },
  //分享
  onShareAppMessage: function() {
    return {
      title: '王者摇骰子',
      desc: '王者摇骰子',
      path: '/pages/index/index'
    }
  },
  //骰子个数事件
  diceCountOpr: function() {
    console.log("diceCountOpr");
    this.setData({
      actionSheetHidden: false
    })
  },
  //点击取消
  actionSheetChange: function() {
    console.log("actionSheetChange")
    this.setData({
      actionSheetHidden: true
    })
  },
  //条目点击事件
  actionSheetItem: function(event) {
    console.log(event)
    this.setData({
      actionSheetHidden: true
    })
    var diceCount = event.currentTarget.dataset.value;
    if (diceCount != 0) {
      this.setData({
        diceCount: diceCount
      });
      var displayFlag = [];
      for (var i = 0; i < 6; i++) {
        if (diceCount - 1 == i) {
          displayFlag[i] = "";
          console.log(displayFlag[i])
        } else {
          displayFlag[i] = "none";
          console.log(displayFlag[i])
        }
        this.setData({
          displayFlag: displayFlag,
          historyCount: 0, //历史点数
          currentCount: 0, //当前点数
          genCount: [], //生成的骰子点数
          dicePng: this.data.initPng
        });
      }
    }
    console.log(this.data.diceCount);
  },
  //摇骰子事件
  diceWaveOpr: function() {
    console.log("diceWaveOpr");
    wx.playBackgroundAudio({
      dataUrl: "http://dx.sc.chinaz.com/Files/DownLoad/sound1/201510/6522.mp3",
    });
    var genCount = [];
    for (var i = 0; i < this.data.diceCount; i++) {
      //点子数
      var num = Math.floor(Math.random() * 6 + 1);
      genCount[i] = num;
    }
    console.log(genCount);
    this.setData({
      genCount: genCount, //生成的骰子点数
      dicePng: this.data.initPng
    });
    wx.vibrateLong({

    });
  },
  //开事件
  diceOpen: function() {
    console.log("diceOpen");
    var diceCount = this.data.diceCount;
    var currentCount = 0;
    var dicePng = this.data.dicePng;
    //生成的点子数
    var genCount = this.data.genCount;
    var len = 6;
    console.log("diceCount = " + diceCount)
    var dicePointPng = [];
    console.log("---genCount---" + genCount)
    console.log(this.data.diceConst)
    if (genCount.length > 0) {
      for (var i = 0; i < len; i++) {
        if (i < diceCount) {
          //生成的点数
          var dicePoint = genCount[i];
          console.log(dicePoint)
          console.log(this.data.diceConst[dicePoint])
          //改变png图片  dicePoint = 图片下标-1  数组从零开始
          dicePointPng[i] = this.data.diceConst[dicePoint-1];
          //点数=数组下标+1
          currentCount = currentCount + dicePoint;
        }
      }
      //变成历史
      var historyCount = this.data.currentCount;
      this.setData({
        historyCount: historyCount, //历史点数
        currentCount: currentCount, //当前点数
        dicePng: dicePointPng,
      });
      console.log(this.data.dicePng);
    } else {
      //提示
      wx.showToast({
        title: '先摇一摇',
        icon: 'success',
        duration: 1000,
        mask: true
      })
    }
  }
})